import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/base/BaseController.dart';
import 'package:xin_nuo_pda/bean/lot_entity.dart';
import 'package:xin_nuo_pda/http/api/lot_api.dart';

import 'DefectClearThingState.dart';

class DefectClearThingController extends BaseController {
  final DefectClearThingState state = DefectClearThingState();

  @override
  void onInit() {
    super.onInit();
    state.actionList.add('留站内');
    state.actionList.add('返工');
    state.actionList.add('报废');
    state.actionList.add('分批');
    state.actionList.add('分批出站');
    state.actionList.add('分批报废');
  }

  void updateSelectDefectValue(String str,int position) {
    state.binData![position].CLEARTYPE = str;
    update([state.dropDownId]);
  }

  getLotInfo(String lot) async{
    Map<String, dynamic> params = {'LOTID': lot};
    var result = await LotApi.lotInfo(canelToken, params: params);
    if (result.ok) {
      state.lotEntity = result.data;
      List<LotEntity> lotList = [];
      lotList.add(state.lotEntity ?? LotEntity());
      Map<String, dynamic> map = {'ACTIONTYPE': 'SelectClearData','EQPID' : state.lotEntity?.eQUIPMENTID,'LOTLIST' : lotList};
      var bindata = await LotApi.getSelectorBin(canelToken,params: map);
      if(bindata.ok){
        state.binDataEntity = bindata.data;
        if(bindata.data!.LOTBINDATALIST!.isNotEmpty){
          state.binData = bindata.data?.LOTBINDATALIST;
        }else{
          Get.snackbar('提示', '服务器返回BIN数据为空！');
        }
        update([state.defectClear]);
      }
    }
  }

  clearThing() async{
    List<LotEntity> lotList = [];
    lotList.add(state.lotEntity ?? LotEntity());
    Map<String, dynamic> params = {'ACTIONTYPE': 'ClearTrackout','LOTLIST' : lotList,'LOTBINDATALIST' : state.binData};
    var result = await LotApi.getSelectorBin(canelToken,params: params);
    if(result.ok){
        Fluttertoast.showToast(msg: '清料成功');
        Get.back();
    }

  }
}
